package com.zr.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.UpdateDisstationDao;
import com.zr.model.Cdisstation;
import com.zr.model.Clog;
import com.zr.model.Cstationtype;
import com.zr.model.Cuser;

@Repository("updateDisstationDao")
public class UpdateDisstationDaoImpl<T> extends BaseDaoImpl<T> implements UpdateDisstationDao<T>{

	
	
	
	
	@Override
	public void insertInfoIntoLog(String time, String uname, int uid, String info) {
		Clog clog = new Clog();
		clog.setLinfo(info);
		clog.setLtime(time);
		clog.setUname(uname);
		clog.setUid(uid);
		this.getCurrentSession().save(clog);
	}

	@Override
	public Cuser getUserByInfo(int uid) {
		String hql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("uid", uid);
		Cuser cd = (Cuser)que.uniqueResult();
		return cd;
	}

	@Override
	public Cstationtype getStationTypeByInfo(String distype, String disstate) {
		String hql = "from Cstationtype where distype = :distype and disstate = :disstate";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("distype", distype);
		que.setString("disstate", disstate);
		Cstationtype cd = (Cstationtype)que.uniqueResult();
		return cd;
	}

	@Override
	public void updateStationByInfo(int disid, String disname, Cuser cuser, Cstationtype cstationtype, String disscope,
			Double disprice, int instation) {
		String hql = "from Cdisstation where disid=:disid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("disid", disid);
		Cdisstation cd = (Cdisstation)que.uniqueResult();
		cd.setCstationtype(cstationtype);
		cd.setCuser(cuser);
		cd.setInstation(instation);
		cd.setDisname(disname);
		cd.setDisprice(disprice);
		cd.setDisscope(disscope);
		this.getCurrentSession().save(cd);
	}

	@Override
	public Cdisstation getOidStationInfoByDisid(int disid) {
		String hql = "from Cdisstation where disid=:disid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("disid", disid);
		Cdisstation cd = (Cdisstation)que.uniqueResult();
		return cd;
	}

	@Override
	public void updateUserInfoByUid(int uid,int disid) {
		System.out.println(disid);
		System.out.println(uid);
		String hql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("uid", uid);
		Cuser u = (Cuser)que.uniqueResult();
		System.out.println(u.getUid());
		u.setDisid(disid);
		this.getCurrentSession().save(u);
	}

	@Override
	public int getStataionExistsOrNot(String disname,int disid) {
		int i = 0;
		List<Cdisstation> ds = new ArrayList<Cdisstation>();
		String hql = "from Cdisstation where disname = :disname and disid not in :disid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("disname", disname);
		que.setInteger("disid", disid);
		ds = que.list();
		if (ds.size()>0) {
			i=1;
		} else {
            i=0;
		}
		return i;
	}

	
		
	

}
